<%@ include file="/common/taglibs.jsp"%>
<!--分页查询共用的页面-->
<%@ include file="/common/common.jsp"%>
<%@ include file="/common/dateUtil.jsp"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<style>
.btnClass{
	width:80px;
	height:30px;
	margin-top: 6px;
}

/*
左侧视频图片区样式*/
.leftImgDiv{
	width:750px;
	height:600px;
	overflow:auto;
}

/*
右侧样式
*/
.rightDiv{
	width:400px;
}

/*
右侧区域列表样式
*/
.rightDivList{
	width:400px;
	overflow:auto;
}

</style>
<script>
//定义参数数据
params = {};
var myDatagrid = null;

/**
 * 格式化datagrid
 */
$(function(){
	//数据表格
	$("#adasAlarmList").datagrid({
		width:"100%",
		height:"540",
		pagination:false,
		singleSelect:true,
		rownumbers:true,
		striped:true,
		// fitColumns: true,
		checkOnSelect: false,
		selectOnCheck: false,
		// onDblClickRow : function(rowIndex,rowData){//双击事件不需要，不定义
		// },
		onSelect:function(rowIndex, rowData){
			// console.log("单击数据行"+rowIndex);
			// console.log(rowData);
			//选中某一行，查看资源
			getAdasAlarmImgsPage(rowIndex,rowData);
			
		}
	});
});

function getAdasAlarmImgsPage(rowIndex,rowData){
	try {
		var plateNo = rowData.plateNo;
		var alarmNo = rowData.alarmNo;
		var param = "?alarmNo=" + alarmNo;
		var url = "<%=ApplicationPath%>/alarmImage/viewAdasAlarmImage.action" + param;
		//提示显示第几行的信息
		showMsg("正在查看第"+(rowIndex+1)+"条报警信息");
		$("#adasAlarmImgDiv").load(url);
		//下面代码为弹出页面代码
		// var title = plateNo + "报警处理-查看";
		// InfoWindow.openChildWindow(url, 870, 570, title);
	} catch (error) {
		console.error("设置页面错误:" + error);
	}
}


//显示消息方法
function showMsg(msg){
	$("#loadMsg").html(msg)
}


//加载完成后执行方法
$(document).ready(function () {
	//获取数据，并填充
	params.alarmIndex = "${alarmIndex}";
	myDatagrid = $("#adasAlarmList");
	getAjaxData();


	//设置下一条点击事件
	$("#btGetNext").click(function(){
		try {
			// console.log("获取下一行");

			//获取总条数
			var length = 0;
			var rowsData = myDatagrid.datagrid("getRows");
			// console.log(rowsData);
			if(rowsData != null){
				length = rowsData.length;
				// console.log("length="+length);
			}
			
			//获取当前行
			var rowDataTemp = myDatagrid.datagrid("getSelected");
			// console.log(rowDataTemp);
			var indexTemp = myDatagrid.datagrid("getRowIndex",rowDataTemp);
			indexTemp++;
			//如果下一行大于等于总条数，那么选中第0 行
			if(indexTemp >= length)
				indexTemp = 0;
			// console.log("下一条的索引="+indexTemp);
			myDatagrid.datagrid("selectRow",indexTemp);
			
		} catch (error) {
			console.error("获取选中行错误："+error);
		}
	});

});



/*
查询数据，并根据返回情况进行数据填充
*/
function getAjaxData() {
	showMsg("正在加载中...")
	$("#adasAlarmList").datagrid("getPager").pagination("loading");
	$.ajax({
		type: "post",
		url: "<%=ApplicationPath%>/activeSafeAlarm/getAdasAlarmListData.action",//请求地址
		dataType: "json",
		data: JSON.stringify(params),
		contentType: "application/json;charset=utf-8",
		success: function (data) {
			//data数据结构只有列表，没有total属性，
			showMsg("加载完成!")
			// addData(data.rows);//处理数据
			$("#adasAlarmList").datagrid('loadData', data);//绑定数据
			$("#adasAlarmList").datagrid("getPager").pagination("loaded");//刷新动画停止

			// console.log("接收报警列表数据->");
			// console.log(data);

			try {
				if(data!= null){
					var lengthTemp = data.length;
					if(lengthTemp != undefined){
						if(lengthTemp > 0){
							$("#adasAlarmList").datagrid("selectRow",0);
							// console.log("选中第零行");
						}
					}
				}
			} catch (error) {
				console.error("初始化选择第0行出错");
			}
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			console.log("请求查询数据错误");
			showMsg("加载发生错误!")
		}
	});

}

function btnClick(){
	window.parent.closeCommandWindow();
}

/**
 * 处理方法
 */
function processAlarm(){
	
	try {
		// console.log("处理")
		var rowDataTemp = myDatagrid.datagrid("getSelected");
		// console.log(rowDataTemp);
		if (rowDataTemp != null) {
			var vehicleId = rowDataTemp.vehicleId;
			var id = parseInt("${id}");
			if (id == undefined)
				id = 0;
			//alarmId的值为alarm表中的id字段的值
			var param = "?input=true&vehicleId="+rowDataTemp.vehicleId+"&alarmId="+id;
			var url = "<%=ApplicationPath%>/sendText/view.action"+param;
			var title = rowDataTemp.plateNo+"文本下发";
			InfoWindow.openChildWindow(url,620, 300, title);
			// console.log(title);
		}
		
	} catch (error) {
		console.error("处理报警信息错误：" + error);
	}

}

function releaseAlram(){
	try {
		// console.log("解除报警")
		var rowDataTemp = myDatagrid.datagrid("getSelected");
		// console.log(rowDataTemp);
		if (rowDataTemp != null) {
			var vehicleId = rowDataTemp.vehicleId;
			var id = parseInt("${id}");
			if (id == undefined)
				id = 0;
			//alarmId的值为alarm表中的id字段的值
			// var param = "?input=true&alarmId="+id+"&processed=1";strAlarmId
			// var url = "<%=ApplicationPath%>/alarm/process.action"+param;
			var param = "?input=true&strAlarmId="+id.toString();
			var url = "<%=ApplicationPath%>/alarm/batchProcess.action"+param;
			$.ajax({
				type: "post",
				url: url,//请求地址
				dataType: "json",
				data:null,
				contentType: "application/json;charset=utf-8",
				success: function (data) {
					if (data.success)
						alert("报警已处理,命令下发给终端!");
					else
						alert("提交失败! 错误:" + data.message);
					// window.parent.closeCommandWindow();
				}
			});
		}
	} catch (error) {
		console.error("解除报警功能错误："+error);
	}
}




</script>

</head>
<body>
	<table class="TableBlock"><!--class="TableBlock"-->
		<tbody>
			<tr>
				<td colspan="2" style="text-align:center;width:100%;height:18px;">
					<!--最上侧的信息显示行-->
					<span id="loadMsg" ></span>
				</td>
			</tr>
			<tr>
				<td class="leftImgDiv">
					<!--左侧视频图片显示区-->
					<div id="adasAlarmImgDiv">
					</div>
				</td>
				<td class="rightDiv">
					<!--右侧报警列表加载区-->
					<div class="rightDivList">
						<table id="adasAlarmList" >
							<thead>
								<tr>
									<th data-options="field:'id',checkbox:true" width="100"><input type='checkbox' id='selectAll'></input></th>
									<th data-options="field:'alarmTime'" width="160">报警时间</th>
									<th data-options="field:'location'" width="260">报警地点</th>
									<th data-options="field:'speed'" width="60">车速(km/h)</th>
								</tr>
							</thead>
						</table>
					</div>
					
					<table style="width:100%;text-align:center;">
						<tbody>
							<tr>
								<td>
									<input type="button" onclick="releaseAlram()" value="解除警报" class="btnClass">&nbsp;&nbsp;
									<input type="button" onclick="processAlarm()" value="文本下发" class="btnClass">&nbsp;&nbsp;
									<input id="btGetNext" type="button"  value="下一条" class="btnClass">&nbsp;&nbsp;
									<input type="button" onclick="btnClick()" value="关闭" class="btnClass"></td>
							</tr>
						</tbody>
					</table>
				</td>
			</tr>
		</tbody>
	</table>
	
</body>

</html>